/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.*;
import static org.junit.Assert.*;

/**
 *
 * @author Eric
 */
public class ItemManagerTest {
    //variables
    ItemManager itemManager;
    
    private ProductContainer unit1;
    private ProductContainer unit2;
    
    private Product product1;
    private Product product2;
    private Product product3;
    
    private Calendar cal1;
    private Calendar cal2;
    
    private Item item1;
    private Item item2;
    
    
    public ItemManagerTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }
    
    @Before
    public void setUp() {
        itemManager = ItemManager.getInstance();
        
        Calendar c = new GregorianCalendar();
        product1 = new Product(c, new Barcode("12345"), "description",new Amount(Unit.count, 1), 0, 0);
        unit1 = new StorageUnit("storage1");
        unit2 = new StorageUnit("storage2");
        cal1 = new GregorianCalendar();
        item1 = new Item(product1, unit1, cal1, itemManager.generateBarcode());
        item2 = new Item(product1, unit2, cal1, itemManager.generateBarcode());
    }
    
    @After
    public void tearDown() {
        ItemManager.removeInstance();
    }

    @Test
    public void testGetInstance() {
        System.out.println("getInstance");
        //1
        ItemManager itemManager2 = ItemManager.getInstance();
        
        assertTrue (itemManager == itemManager2);
        
        //2
        itemManager.addItem(item1);
        try {
            assertTrue (itemManager.getAllItems().size() == itemManager2.getAllItems().size());
        } catch (Exception e) {
            fail();
        }
        itemManager.addItem(item2);
        try {
            assertTrue (itemManager.getAllItems().size() == itemManager2.getAllItems().size());
        } catch (Exception e) {
            fail();
        }
    }

    @Test
    public void testAddAndRemoveItem() {
        System.out.println("addItem");
        //1
        itemManager.addItem(item1);
        
        Collection<Item> items = itemManager.getAllItems();
        assertTrue(items.size() == 1);
        assertTrue(items.contains(item1));
        
        itemManager.addItem(item2);
        
        items = itemManager.getAllItems();
        assertFalse(items.size() == 1);
        assertTrue(items.size() == 2);
        assertTrue(items.contains(item2));
        
        
        //2
        try{
            itemManager.removeItem(item1);
        }
        catch(Exception e)
        {
            fail();
        }
        assertTrue(items.size() == 1);
        
        //2.5
        try {
            assertTrue(itemManager.getRemovedItems().contains(item1));
        } catch (Exception e) {
            fail();
        }
        assertFalse(itemManager.getAllItems().contains(item1));
        
        //3
        try{
            itemManager.removeItem(item2.getBarcode());
        }
        catch(Exception e)
        {
            assertTrue(true);
        }
        assertTrue(!items.isEmpty());
        
    }

    @Test
    public void testAddItemToContainer() throws Exception {
        System.out.println("addItemToContainer");
        //1
        boolean success = false;
        try
        {
            itemManager.addItemToContainer(null, unit1);
        }
        catch(Exception e)
        {
            success = true;
        }
        assertTrue(success);
        
        //2
        success = false;
        try
        {
            itemManager.addItemToContainer(item1, null);
        }
        catch(Exception e)
        {
            success = true;
        }
        assertTrue(success);
        
        //3
        itemManager.addItem(item1);
        success = false;
        try
        {
            itemManager.addItemToContainer(item2, unit1);
        }
        catch(Exception e)
        {
            success = true;
        }
        assertTrue(success);
        
        success = false;
        try
        {
            itemManager.addItemToContainer(item1, unit1);
        }
        catch(Exception e)
        {
            fail();
        }
        assertTrue(itemManager.getAllItemsInPC(unit1).contains(item1));
        
        
    }


    @Test
    public void testCanRemoveItem() {
        System.out.println("canRemoveItem");
        assertTrue(itemManager.canRemoveItem(item1));
    }

    @Test
    public void testCanRemoveContainer() throws Exception {
        System.out.println("canRemoveContainer");
        //1
        boolean success = false;
        try{
            itemManager.canRemoveContainer(null);
        }
        catch(Exception e)
        {
            success = true;
        }
        assertTrue(success);
        
        
        //2
        itemManager.addItem(item1);
        try
        {
            assertFalse(itemManager.canRemoveContainer(unit1));
        }
        catch(Exception e)
        {
            fail();
        }
        
        //3
        success = false;
        try{
            assertTrue(itemManager.canRemoveContainer(unit2));
        }
        catch(Exception e)
        {
            success = true;
        }
        assertTrue(success);
    }

    @Test
    public void testGetRemovedItems() throws Exception {
        System.out.println("getRemovedItems");

        assertTrue(itemManager.getRemovedItems().isEmpty());
        itemManager.addItem(item1);
        assertTrue(itemManager.getRemovedItems().isEmpty());
        itemManager.removeItem(item1);
        assertFalse(itemManager.getRemovedItems().isEmpty());
        
    }

    @Test
    public void testCanRemoveProduct() throws Exception {
        System.out.println("canRemoveProduct");
        //1
        boolean success = false;
        try{
            itemManager.canRemoveProduct(null);
        }
        catch(Exception e)
        {
            success = true;
        }
        
        //2
        success = false;
        try{
            if(itemManager.canRemoveProduct(item1.getProduct()))
            {
                success = true;
            }
        }
        catch(Exception e)
        {
            fail();
        }
        assertTrue(success);
        
        //3
        success = false;
        itemManager.addItem(item1);
        try{
            if(itemManager.canRemoveProduct(item1.getProduct()))
            {
                success = true;
            }
        }
        catch(Exception e)
        {
            fail();
        }
        assertFalse(success);
        
        
    }

    @Test
    public void testEditItem() throws Exception {
        System.out.println("modifyItem");
        
        
    }


    @Test
    public void testFindItem() throws Exception {
        System.out.println("findItem");
        //1
        boolean success = false;
        try{
        	String test = null;
            itemManager.getItem(test);
        }
        catch(Exception e)
        {
            success = true;
        }
        assertTrue(success);
        
        //2
        itemManager.addItem(item1);
        success = false;
        Item i;
        try{
            i = itemManager.getItem(item1.getBarcode());
            assertTrue(i.getBarcode().equals(item1.getBarcode()));
        }
        catch(Exception e)
        {
            success = true;
        }
    }
}
